layui.use(['layer', 'form', 'element', 'jquery', 'dialog'], function () {
    var layer = layui.layer;
    var element = layui.element;
    var form = layui.form;
    var $ = layui.jquery;
    var dialog = layui.dialog;
    var hideBtn = $('#hideBtn');
    var mainLayout = $('#main-layout');
    var mainMask = $('.main-mask');
    //监听左侧导航点击
    element.on('nav(leftNav)', function (elem) {
        var navA = $(elem)//.find('a');
        var id = navA.attr('data-id');
        var url = navA.attr('data-url');
        var text = navA.attr('data-title');
        if (!url) {
            return;
        }
        var isActive = $('.main-layout-tab .layui-tab-title').find("li[lay-id=" + id + "]");
        if (isActive.length > 0) {
            //切换到选项卡
            element.tabChange('tab', id);
            CustomRightClick(id); //给tab绑定右击事件
        } else {
            element.tabAdd('tab', {
                title: text,
                content: '<iframe src="' + url + '" name="iframe' + id + '" class="iframe" framborder="0" data-id="' + id + '" scrolling="auto" width="100%"  height="100%"></iframe>',
                id: id
            });
            element.tabChange('tab', id);
            CustomRightClick(id); //给tab绑定右击事件
        }
        mainLayout.removeClass('hide-side');
    });

    //左侧导航隐藏显示
    hideBtn.on('click', function () {
        if (!mainLayout.hasClass('hide-side')) {
            mainLayout.addClass('hide-side');
        } else {
            mainLayout.removeClass('hide-side');
        }
    });
    //遮罩点击隐藏
    mainMask.on('click', function () {
        mainLayout.removeClass('hide-side');
    })

    // 绑定tab右键事件
    function CustomRightClick(id) {
        //取消右键  rightmenu属性开始是隐藏的 ，当右击的时候显示，左击的时候隐藏
        $('.layui-tab-title li').on('contextmenu', function () {
            return false;
        })
        $('.layui-tab-title,.layui-tab-title li').click(function () {
            $('.rightmenu').hide();
        });
        //桌面点击右击
        $('.layui-tab-title li').on('contextmenu', function (e) {
            var popupmenu = $(".rightmenu");
            popupmenu.find("li").attr("data-id", id); //在右键菜单中的标签绑定id属性

            //判断右侧菜单的位置
            l = ($(document).width() - e.clientX) < popupmenu.width() ? (e.clientX - popupmenu.width()) : e.clientX;
            t = ($(document).height() - e.clientY) < popupmenu.height() ? (e.clientY - popupmenu.height()) : e.clientY;
            popupmenu.css({left: l, top: t}).show(); //进行绝对定位
            //alert("右键菜单")
            return false;
        });
    }


    // 监听tab点击切换
    // $('.layui-tab-title').on('click', 'li', function () {
    //     var id = $(this).attr('lay-id');
    //     console.log($('.nav-list').find("a[data-id=" + id + "]").parent('li'));
    //     $('.nav-list').find("li[data-id=" + id + "]").parent('li').addClass('layui-this').siblings('li').removeClass('layui-this')
    // })

});
